\documentclass[11pt]{article}
%% \topmargin -1.5cm        % read Lamport p.163
%% \oddsidemargin -0.04cm   % read Lamport p.163
%% \evensidemargin -0.04cm  % same as oddsidemargin but for left-hand pages
%% \textwidth 16.59cm
%% \textheight 21.94cm 
%$ %\pagestyle{empty}       % Uncomment if don't want page numbers
\parskip 7.2pt           % sets spacing between paragraphs
%% %\renewcommand{\baselinestretch}{1.5} % Uncomment for 1.5 spacing between lines
\parindent 0pt		 % sets leading space for paragraphs

\usepackage{fullpage}
\usepackage{listings} % For source code
\usepackage[usenames,dvipsnames]{color} % For colors and names
\usepackage[pdftex]{graphicx}
\usepackage{subfigure}

\newcommand{\superscript}[1]{\ensuremath{^{\textrm{#1}}}}
\newcommand{\subscript}[1]{\ensuremath{_{\textrm{#1}}}}

\definecolor{mygrey}{gray}{.96} % Light Grey
\lstset{ 
        language=[ISO]C++,              
        tabsize=3,                                                     
        basicstyle=\tiny,               
        numbers=left,                   
        numberstyle=\tiny,              
        stepnumber=2,                   
        numbersep=5pt,                  
        backgroundcolor=\color{white}, 
        %showspaces=false,              
        %showstringspaces=false,        
        %showtabs=false,                
        frame=single,                    
        tabsize=3,                          
        captionpos=b,                   
        breaklines=true,                
        breakatwhitespace=false,        
        %escapeinside={\%*}{*)},        
        commentstyle=\color{BrickRed}   
}

\title{Northeastern University \\
  Department of Electrical and Computer Engineering \\
  - \\
  ECE5667 \\
  Lab 2 - Introduction to the Talkthrough Program}
\date{\today}
\author{
  Instructor: Deniz Erdogmus \\
  TA: Hooman Nezamfar \\
  Lab Partner: Andrew Lai \\
  - \\
  Author's Name: Paul Ozog
}

\begin{document}

\begin{titlepage}
  \maketitle
  \thispagestyle{empty}
\end{titlepage}

\tableofcontents
\pagebreak

\section{Introduction}
In this lab, we learned how to use the A/D and D/A features of the ADSP-BF535 EZ-Kit Lite DSP board.  We based our experiments on the {\bf Talkthrough} program included in the VisualDSP++ development environment.  Using this program, we sampled and quantized the input signal, and passed the samples to the output of the board for qualitative and quantitative analysis.  We adjusted various registers of the AD1885 codec chip to learn how to use the board effectively for subsequent labs. 

\section{Results and Analysis}

\subsection{Exercise 2 : Running the Talkthrough program}
The result of the {\bf Talkthrough} program running with the default register values are shown in {\bf Figure \ref{00}}.  Due to a mistake in setting the oscilloscope, the Output wave's vertical scale is twice that of the Input wave's.  However, the two waves share approximately the same amplitude, but the Output is a bit noisier and $\pi$/2 radians out of phase.

\begin{figure}[p]
  \begin{center}
    \includegraphics[width=120mm]{PRINT_00.png}
    \caption{V\subscript{in} vs. V\subscript{out}: f = 950Hz, V_{iP-P} = 300mV $\pm$ 30mV, V_{oP-P} = 350mV $\pm$ 30mV}
    \label{00}
  \end{center}
\end{figure}

For {\bf Figure \ref{00}}, the input to the board was taken from the output of our function generator.  The generator's relevant settings were as follows:

\begin{itemize}
\renewcommand{\labelitemi}{$\cdot$}
\item {Frequency: 953Hz}
\item {Peak to Peak Voltage: 320mV (always below 500mV to prevent damage to the board)}
\item {Wave type: Sinusoid}
\end{itemize}

Just by examining {\bf Figure \ref{00}}, the basic function of the Talkthrough program becomes clear: sample and quantize the input using the AD1885 codec's ADC, and pass those samples to the DAC and finally the output jack.  In the next exercises, we begin to adjust parameters of the codec for quantitative and qualitative analysis.

\subsection{Exercise 3 - 4 : Understanding the Codec Registers}
Here, we dedicate a short subsection to each register we attempted to modify.  We give appropriate analysis for each register.  The registers responsible for sample rates are covered in a separate section (see {\bf Section \ref{ex4}}). 

\subsubsection{PCM\_OUT\_VOLUME}
\label{PCM}
The default value of \texttt{PCM\_OUT\_VOLUME} (address 0x1800) was 0x0808.  According to the AD1885 documentation, the MSB of this register controls if the output is muted.  Therefore, writing 0x8808 to this register and restarting {\bf Talkthrough}, we expect the output voltage to remain at 0 (plus noise).  The results are shown in {\bf Figure \ref{01}}.

\begin{figure}[p]
  \begin{center}
    \includegraphics[width=120mm]{PRINT_01.png}
    \caption{V\subscript{in} vs. V\subscript{out}: Output is muted}
    \label{01}
  \end{center}
\end{figure}

Clearly, the output is muted due to our modification of the \texttt{PCM\_OUT\_VOLUME} register.

\subsubsection{MASTER\_VOLUME}
The MASTER\_VOLUME register had a similar effect to the PCM\_OUT\_VOLUME register (see {\bf Section \ref{PCM} and Figure \ref{01}}).  When set to a gain of -94.5 dB (ie - very high attenuation), the output waveform's amplitude became very small.

\subsubsection{LINE\_IN\_VOLUME}
We expected this register to decrease the input wave's amplitude.  However, we could not get this register to have any effect (despite the RECORD\_SELECT register being correctly set to Line-In - see {\bf Section \ref{rec_sel}}).  Even when set to mute (0x8808), the input showed now attenuation (similar to {\bf Figure \ref{00}}).

\subsubsection{MIC\_VOLUME}
\label{MIC}
Though we attempted to modify this register (address 0x0E00) to mute the input, any modifications didn't yield any difference from {\bf Figure \ref{00}}.  This is because the RECORD\_SELECT register (see {\bf Section \ref{rec_sel}}) was set to use Line-In as the input (instead of Mic), and therefore this register is essentially ignored.  

\subsubsection{RECORD\_SELECT}
\label{rec_sel}
The RECORD\_SELECT register (address 0x1A00) is used to select different input types from the 1/8'' input jack.  Though we tried setting this register to use the MIC as an input (by writing 0x0000 instead of 0x0404), this had no effect.  We concluded that this was because the LINE-IN/MIC jumper was set to LINE-IN (see the ADSP-BF535 EZ-Kit manual).  Concerned with the possibility of damaging the board with electrostatic discharge, we decided to leave the input select jumper at LINE-IN for the duration of the lab.  

\subsection{Exercise 5 : PCM\_DAC\_RATE\_0/1 with Identical Sample Rates}
\subsubsection{Quantitative Results}
\label{ex4}
To measure the performance of the Talkthrough program, we had the output of the function generator and output of the EZ-Kit board go to the oscilloscope.  As shown in {\bf Figures \ref{03} and \ref{04}}, there is little attenuation for frequencies below half the sample rate, and large attenuation for frequencies above half the sample rate.  

This is explained by the Nyquist Criterion, which states that any signal can be recovered from evenly distributed samples if the maximum frequency of the input signal is less than or equal to twice the sampling frequency:

\begin{equation}
f_{max} \leq {f_s \over 2}
\end{equation}

In the case where the sample rate is set to 7.040kHz:

\begin{equation}
f_{max} \leq 3.52kHz
\end{equation}

and for a 48kHz sample rate:
\begin{equation}
f_{max} \leq 24.0kHz
\end{equation}

Setting the ADC and DAC to have a sample rate of 7.040kHz (as in the following figures) is done by writing the value 0x1B80 to the PCM\_DAC\_RATE\_0/1 registers.  

\begin{figure}[h]
  \begin{center}
    \includegraphics[width=120mm]{PRINT_03.png}
    \caption{V\subscript{in} vs. V\subscript{out}: Nyquist Criterion Satisfied (f_{in} = 2.62kHz)}
    \label{03}
  \end{center}
\end{figure}

\pagebreak 

\begin{figure}[h]
  \begin{center}
    \includegraphics[width=120mm]{PRINT_04.png}
    \caption{V\subscript{in} vs. V\subscript{out}: Nyquist Criterion {\bf not} Satisfied (f_{in} = 3.62kHz)}
    \label{04}
  \end{center}
\end{figure}

\subsubsection{Qualitative Results}
When maximizing the sample rate to 48kHz (by writing 0xBB80 to the PCM\_DAC\_RATE\_0/1 registers), we qualitatively used the speakers to discern when the Nyquist Criterion is not satisfied.  When the function generator was increased to 24.2kHz, we could no longer hear the tone coming out of the speakers.  All frequencies below 24.2kHz were reproduced by the DAC clearly with little attenuation.  These results are consistent with the Nyquist Criterion described in the previous sections.

When minimizing the sample rate, the sound quality of the EZ-Kit output lacks any high end frequencies when using an MP3 player as an input.  This observation makes sense given the attenuation observed and analyzed in the previous section. 

\subsection{Exercise 5 : PCM\_DAC\_RATE\_0/1 with Different Sample Rates}
\subsubsection{Qualitative Results}
When the ADC sample rate is 48kHz and the DAC sample rate is 7.04kHz, the sound quality lacks any high-end frequencies.  Interestingly, when the ADC is 7.04kHz and the DAC is 48kHz, the sound lacks high-end and is more distorted.  To our ears, the result sounded a bit ``crackly.'' 

These results show that there is little reason to use different ADC/DAC sample rates, and thus the PCM\_DAC\_RATE\_0/1 registers should be set to the same value. 

\subsection{Exercise 6 : LED control}
After building and running the {\bf led.c} program, every other LED lit for a short but noticeable delay, and then the OFF LEDs turn ON and vice-versa.  Looking at the source code, it's clear that each bit in the least-significant hexadecimal digit turns OFF the respective LED when high, and ON when low.

Therefore, to make the alternating pattern called for the in the manual, we simply had to write the hexadecimal number 0x0006 (LSD: 0110) and 0x0009 (LSD: 1001) to the register pointed to by \texttt{fio\_clr\_reg} instead of 0x000A (LSD: 1010) and 0x0005 (LSD: 0101).

Similarly, to have the each LED turn ON one at a time, we used the hexadecimal values 0x000E, 0x000D, 0x000B, and 0x0007 in between the delays and clearing of the LEDs.

Finally, to increase the delay time, there are multiple approaches.  One could increase \texttt{DELAY\_LOOP}, use more ``nop'' instructions, or call \texttt{delay()} consecutively in \texttt{main()}.  By default, there is only one call to \texttt{delay()} in between setting the LEDs.

\subsection{Answers to Explicit Questions in Lab manual}
{\bf Q: Comment on what you learn from both manuals.}

{\bf A:}  The AD1885 manual serves primarily as a reference for the control registers used in this and subsequent labs.  It also has a nice diagram on the first page, so one can see how blocks of the AD1885 interact with each other.

By examining both the Talkthough ``Main.c'' file and the ADSP-BF535 Blackfin Processor Hardware Reference, it's clear that the Talkthrough program in set up to use SPORT0 in the ``Descriptor Multichannel mode''.  In Talkthrough, there is a buffer for both the RX and TX side to store samples from/to the ADC/DAC, respectively.

{\bf Q: What’s the purpose of the “while(1) \{...\}” instruction?}

{\bf A:}  \texttt{main()} must run continuously to pass samples from the ADC to the DAC.  If the infinite loop wasn't there, there would only be one sample taken from input to output, and the program would exit.

{\bf Q: Any relationships you notice between eCodec\_Registers definition and the descriptions for the codec in the manual on page 11?}

{\bf A:}  The first two digits of the addresses correspond to the Index of the AD1885 control registers.  The eCodec\_Registers names are obviously very similar to those of the codec manual for ease of readability, but the most important values in the eCodec\_Registers enumeration are the hex values.  

{\bf Where do you think your later DSP operations will be inserted?}  

{\bf A:}  The file ``BF535 Talkthrough.h'' declares a \texttt{Process\_Audio\_Data} function, which is implemented in the file ``Process Audio Data.c.''  This function is where much of the subsequent labs will be focused.  

\section{Conclusions}
One of the major accomplishment of this lab is that we've shown through manipulation of the AD1885 control registers that the Nyquist Criterion holds for frequencies below 3.62kHz (when the sampling rate is 7.04kHz) and 24.2kHz (when the sampling rate is 48kHz).  However, it is important to realize that higher sampling rates aren't always ``better''; a higher sampling rate may give a wider bandwidth, but it also gives less time for DSP calculations in between samples.  

We've also shown that the Nyquist concept has a qualitative effect on our experiments: high-end frequencies of music passed into the BF535 are not preserved at the output.  So, using the knowledge gained from the AD1885 control registers and sampling, we can effectively use the DSP board for later labs where these concepts become essential for our chosen input signal. 

Finally, we've demonstrated a working knowledge of the row of LEDs on the EZ-Kit Lite.  These may prove useful for applications of the BF535 that ought to provide visual feedback, and not just processing samples with no visual information.
\end{document}
